package com.itheima.mm.controller;

import com.itheima.mm.dao.CatalogDao;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.entity.Result;
import com.itheima.mm.pojo.Catalog;
import com.itheima.mm.pojo.Course;
import com.itheima.mm.pojo.Question;
import com.itheima.mm.service.CourseService;
import com.itheima.mm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping("/findListAll")
    public Result findListAll(){
        List<Course> courseList= courseService.findListAll();
        return new Result(true,"获取列表成功",courseList);
    }

    @PostMapping("/findListByPage")
    public Result findListByPage(@RequestBody QueryPageBean queryPageBean){
        List<Course> courseList= courseService.findListByPage(queryPageBean);
        /*获取所有题目的问题*/
        Long total = courseService.findTotal(queryPageBean);
        return new Result(true,"获取题目列表成功",new PageResult(total,courseList));
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam("courseId") Integer courseId){
        boolean delete = courseService.delete(courseId);
        if(delete){
            return new Result(true,"删除成功");
        }else {

            return new Result(false,"请检查二级目录");
        }

    }
    @PostMapping("/update")
    public Result update(@RequestBody Course course){
        courseService.update(course);
        return new Result(true,"更新成功");
    }

    @PostMapping("add")
    public Result add(@RequestBody Course course){
        course.setUserId(1);
        course.setCreateDate(DateUtils.parseDate2String(new Date()));
        courseService.add(course);

        return new Result(true,"添加成功");
    }


}
